<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/9
 * Time: 18:19
 */

namespace App\HttpController\V1;

use App\Tools\Token\Token;
use EasySwoole\EasySwoole\Config;
class Goods extends BaseWithDb
{
    public $user;
    /**
     * 推荐轮播图
     * @return bool
     */
    function bannlist(){
        $bannlist=$this->db->where('type',2)->orderBy('sort','desc')->get('banner',null,'id,image');
        foreach ($bannlist as $key=>&$val){
            $val['image']=QINIU_URL.$val['image'];
        }
        return $this->writeJson(200,$bannlist,'成功');
    }

    /**
     * 导航分类
     */
    function classIndex(){
        $data=$this->db->where('pid',4)->get('study_class',null,'id,class_name');
        return $this->writeJson(200,$data,'成功');
    }

    /**
     * 商品列表
     * @return bool
     */
    function goodsList(){
        $id=$this->input('id');
        $page=$this->getPage();
        switch ($id){
            case ''||null:
                $list['banner']=$this->db->where('type',1)->orderBy('sort','desc')->get('banner',null,'id,image');
                if($list['banner']!=null){
                    foreach ($list['banner'] as $ks=>&$vs){
                        $vs['image']=QINIU_URL.$vs['image'];
                    }
                }
                $list['new']=$this->db->orderBy('id')->get('goods',6,'id,vip_price,goods_name,goods_pic');
                if($list['new']!=null){
                    foreach ($list['new'] as $kss=>&$vss){
                        $vss['goods_pic']=QINIU_URL.$vss['goods_pic'];
                    }
                }
                $list['recom']=$this->db->where('recomm',1)->orderBy('id')->get('goods',$page,'id,vip_price,goods_name,goods_pic');
                if($list['recom']!=null){
                    foreach ($list['recom'] as $ksss=>&$vsss){
                        $vsss['goods_pic']=QINIU_URL.$vsss['goods_pic'];
                    }
                }
                break;
            default:
                $list['list']=$this->db->where('class_id',$id)->orderBy('id')->get('goods',$page,'id,vip_price,goods_name,goods_pic');
                if($list['list']!=null){
                    foreach ($list['list'] as $ksssss=>&$vssss){
                        $vssss['goods_pic']=QINIU_URL.$vssss['goods_pic'];
                    }
                }
                break;
        }
        $list['count']=0;
        $list['read']=1;
        $jwt = $this->request()->getHeader( Config::getInstance()->getConf('TOKEN_NAME'));

        if ( !empty( $jwt ) ) {
            $data = Token::decode( $jwt[0]);
            $this->user=$data['content'];
            $shop_car=$this->db->where('user_id',$this->user->id )->get('shop_car');
            if($shop_car){
                foreach($shop_car as $key=>$val){
                    $list['count']+=$val['num'];
                }
            }
            $data=$this->db
                ->get('guide');
            foreach ($data as $k=>&$v){
                $read=$this->db->where('guide_id',$v['id'])->where('user_id',$this->user->id)->has('guide_read');
                if(!$read){
                    $list['read']=0;
                    break;
                }
            }
        }
        return  $this->writeJson(200,$list,'查询成功');
    }

    /**
     * 商品详情
     */
    function goodsDetail(){

        $id=$this->input('id');
        if(empty($id)){
            return $this->writeJson(-2,null,'参数错误');
        }
        $goods=$this->db->where('id',$id)->getOne('goods','id,vip_price,market_price,goods_name,goods_images,goods_pic,content,details');
        $goods['count']=0;
        $goods['goods_pic']=QINIU_URL.$goods['goods_pic'];
        if($goods['goods_images']){
            $images=explode(',',$goods['goods_images']);
            foreach($images as &$val){
                $val=QINIU_URL.$val;
            }
            $goods['goods_images']=$images;
        }
        $jwt = $this->request()->getHeader( Config::getInstance()->getConf('TOKEN_NAME') );
        if ( !empty( $jwt ) ) {
            $data = Token::decode( $jwt[0]);
            $this->user=$data['content'];
            $list=$this->db->where('user_id',$this->user->id)->get('shop_car');
            if($list){
                foreach($list as $key=>$val){
                    $goods['count']+=$val['num'];
                }
            }
        }

        return $this->writeJson(200,$goods,'成功');
    }

    /**
     * 搜索
     */
    function search(){
        $word=$this->input('word');
        $page=$this->input('page') ?: 1;
        $page_size=10;
        $info=$this->db->whereLike('goods_name','%'.$word.'%')->get('goods',[($page-1)*$page_size,$page_size],'id,vip_price,goods_name,goods_pic');
        foreach ($info as $k=>&$v){
            $v['goods_pic']=QINIU_URL.$v['goods_pic'];
        }
        return $this->writeJson(200,$info,'成功');
    }


}